#pragma once
#include"task.hpp"
#include <iostream>
#include<functional>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <sys/wait.h>
#include <vector>

const int gprocess_num = 5;
using callback_t =std::function<void(int fd)>;

class channel
{
public:
    channel()
    {}
    channel(int wfd, std::string name, pid_t id)
        : _wfd(wfd), _name(name), _sub_target(id)
    {
    }
    int fd() { return _wfd; }
    std::string name() { return _name; }
    pid_t target() { return _sub_target; }
    void Close()
    {
        close(_wfd);
    }
    void Wait()
    {
        pid_t rid = waitpid(_sub_target, nullptr, 0);
        (void)rid;
    }
    ~channel(){}
private:
    int _wfd;
    std::string _name;
    pid_t _sub_target;
};

class Processpool
{
private:
    void CtrlSubProcessHelper(int &index)
    {
        // 1. 选择一个通道(进程)
        int who = index;
        index++;
        index %= _channels.size();

        // 2. 选择一个任务，随机
        int x = rand() % tasks.size(); // [0, 3]

        // 3. 任务推送给子进程
        std::cout << "选择信道: " << _channels[who].name() << ", subtarget : " << _channels[who].target() << std::endl;
        write(_channels[who].fd(), &x, sizeof(x));
        sleep(1);
    }
public:
    Processpool(int num=gprocess_num) : _processnum(num)
    {
        srand(time(0));
    }

    bool InitProcessPool(callback_t cb)
    {
        for (int i = 0; i < _processnum; i++)
        {
            int pipefd[2] = {0};
            int n = pipe(pipefd);
            if (n < 0)
            {
                perror("pipe");
                return false;
            }
            pid_t id = fork();
            if (id < 0)
            {
                perror("fork");
                return false;
            }
            else if (id == 0)
            {
                close(pipefd[1]);
                // cb(pipefd[0]);
                cb(pipefd[0]);

                exit(0);
            }
            close(pipefd[0]);
            std::string name = "channel-" + std::to_string(i);
            _channels.emplace_back(pipefd[1], name, id);
        }
        return true;
    }

    void PollingCtrlSubProcess()
    {
        int index = 0;
        while (true)
        {
            CtrlSubProcessHelper(index);
        }
    }

    void PollingCtrlSubProcess(int count)
    {
        if (count < 0)
            return;
        int index = 0;
        while (count)
        {
            CtrlSubProcessHelper(index);
            count--;
        }
    }

     void WaitSubProcesses()
    {
        // for(int end = _channels.size()-1; end >= 0; end--)
        // {
        //     _channels[end].Close();
        //     _channels[end].Wait();
        // }
        // for (auto &c : _channels)
        // {
        //     c.Close();
        //     c.Wait();
        // }
        // 1. 先让所有子进程结束
        for (auto &c : _channels)
        {
            c.Close();
        }
        // 2. 你在回收所有的子进程僵尸状态
        for (auto &c : _channels)
        {
            c.Wait();
            std::cout << "回收子进程: " << c.target() << std::endl;
        }
    }
private:
    std::vector<channel> _channels;
    int _processnum;
};
